package com.ruoyi.etravel.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.etravel.pojo.dto.CommentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.base.BaseService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.etravel.mapper.CommentMapper;
import com.ruoyi.etravel.pojo.entity.Comment;
import com.ruoyi.etravel.service.ICommentService;

/**
 * 评论相关功能Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-11
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper,Comment> implements ICommentService
{
    @Autowired
    public CommentMapper commentMapper;

    /**
     * 删除标记
     * @param type
     * @return
     * @param <T>
     */
    public <T> QueryWrapper<T> createSelWrapper(Class<T> type) {
        QueryWrapper<T> queryWrapper = Wrappers.query();
        queryWrapper.eq("del_flag", "0");
        return queryWrapper;
    }

    /**
     * 查询评论相关功能
     *
     * @param commentId 评论相关功能主键
     * @return 评论相关功能
     */
    @Override
    public Comment selectCommentByCommentId(Integer commentId)
    {
        return commentMapper.selectById(commentId);
    }

    /**
     * 查询评论相关功能列表
     *
     * @param comment 评论相关功能
     * @return 评论相关功能
     */
    @Override
    public List<Comment> selectCommentList(Comment comment)
    {
        QueryWrapper<Comment> queryWrapper = createSelWrapper(Comment.class);
        queryWrapper.lambda()
                .eq(StringUtils.isNotNull(comment.getCommentId()), Comment::getCommentId, comment.getCommentId())
                .eq(StringUtils.isNotNull(comment.getTime()), Comment::getTime, comment.getTime())
                .eq(StringUtils.isNotEmpty(comment.getUser()), Comment::getUser, comment.getUser())
                .eq(StringUtils.isNotEmpty(comment.getContent()), Comment::getContent, comment.getContent())
                .eq(StringUtils.isNotNull(comment.getGrader()), Comment::getGrader, comment.getGrader())
                .eq(StringUtils.isNotNull(comment.getScenicId()), Comment::getScenicId, comment.getScenicId())
                .eq(StringUtils.isNotEmpty(comment.getDelFlag()), Comment::getDelFlag, comment.getDelFlag())
        ;
        return commentMapper.selectList(queryWrapper);
    }

    /**
     * 新增评论相关功能
     *
     * @param comment 评论相关功能
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        comment.setCreateBy(SecurityUtils.getUsername());
        comment.setCreateTime(DateUtils.getNowDate());
        comment.setUpdateBy(SecurityUtils.getUsername());
        comment.setUpdateTime(DateUtils.getNowDate());
        int rows = commentMapper.insert(comment);
        return rows;
    }

    /**
     * 修改评论相关功能
     *
     * @param comment 评论相关功能
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        comment.setUpdateBy(SecurityUtils.getUsername());
        comment.setUpdateTime(DateUtils.getNowDate());

        return commentMapper.updateById(comment);
    }

    /**
     * 批量删除评论相关功能
     *
     * @param commentIds 需要删除的评论相关功能主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentIds(Integer[] commentIds)
    {
        return commentMapper.deleteBatchIds(Arrays.asList(commentIds));
    }

    /**
     * 删除评论相关功能信息
     *
     * @param commentId 评论相关功能主键
     * @return 结果
     */
    @Override
    public int deleteCommentByCommentId(Integer commentId)
    {
        return commentMapper.deleteById(commentId);
    }

    /**
     * 根据提供的内容查询符合条件的评论信息
     * @param pageNum 页码
     * @param pageSize 每页展示数量
     * @param commentDto
     * @return
     */
    @Override
    public AjaxResult pageCommentList(int pageNum, int pageSize, CommentDto commentDto) {
        if (pageNum > 0 && pageSize > 0){
            Page<Comment> page = lambdaQuery()
                    .like(StringUtils.isNotEmpty(commentDto.getContent()), Comment::getContent, commentDto.getContent())
                    .eq(StringUtils.isNotNull(commentDto.getGrader()), Comment::getGrader, commentDto.getGrader())
                    .like(StringUtils.isNotEmpty(commentDto.getUser()), Comment::getUser, commentDto.getUser())
                    .page(new Page<>(pageNum, pageSize));
            return AjaxResult.success("成功",page);
        }else {
            return AjaxResult.error("页码数值或者每页显示数量不正确，请重新输入");
        }
    }
}
